package com.water.note.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.water.note.common.PageResult;
import com.water.note.common.Result;
import com.water.note.dto.TodoDTO;
import com.water.note.entity.Todo;
import com.water.note.service.TodoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 待办事项控制器
 */
@RestController
@RequestMapping("/todo")
public class TodoController {
    
    @Autowired
    private TodoService todoService;
    
    /**
     * 创建待办事项
     */
    @PostMapping
    public Result<TodoDTO> createTodo(@RequestBody Todo todo) {
        // 设置当前用户ID
        todo.setUserId(StpUtil.getLoginIdAsLong());
        TodoDTO result = todoService.createTodo(todo);
        return Result.success(result);
    }
    
    /**
     * 更新待办事项
     */
    @PutMapping("/{id}")
    public Result<TodoDTO> updateTodo(@PathVariable Long id, @RequestBody Todo todo) {
        // 设置ID和当前用户ID
        todo.setId(id);
        todo.setUserId(StpUtil.getLoginIdAsLong());
        TodoDTO result = todoService.updateTodo(todo);
        return Result.success(result);
    }
    
    /**
     * 删除待办事项
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteTodo(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = todoService.deleteTodo(id, userId);
        return Result.success(result);
    }
    
    /**
     * 获取待办事项详情
     */
    @GetMapping("/{id}")
    public Result<TodoDTO> getTodoById(@PathVariable Long id) {
        TodoDTO result = todoService.getTodoById(id);
        return Result.success(result);
    }
    
    /**
     * 分页查询待办事项
     */
    @GetMapping("/page")
    public Result<PageResult<TodoDTO>> pageTodos(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword) {
        Long userId = StpUtil.getLoginIdAsLong();
        System.out.println("userid"+userId);
        PageResult<TodoDTO> result = todoService.pageTodos(page, pageSize, userId, status, keyword);
        return Result.success(result);
    }
    
    /**
     * 查询今日待办事项
     */
    @GetMapping("/today")
    public Result<List<TodoDTO>> listTodayTodos() {
        Long userId = StpUtil.getLoginIdAsLong();
        List<TodoDTO> result = todoService.listTodayTodos(userId);
        return Result.success(result);
    }
    
    /**
     * 查询已过期待办事项
     */
    @GetMapping("/overdue")
    public Result<List<TodoDTO>> listOverdueTodos() {
        Long userId = StpUtil.getLoginIdAsLong();
        List<TodoDTO> result = todoService.listOverdueTodos(userId);
        return Result.success(result);
    }
    
    /**
     * 更新待办事项状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateTodoStatus(@PathVariable Long id, @RequestParam Integer status) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = todoService.updateTodoStatus(id, userId, status);
        return Result.success(result);
    }
    
    /**
     * 更新提醒状态
     */
    @PutMapping("/{id}/remind")
    public Result<Boolean> updateRemindStatus(@PathVariable Long id, @RequestParam Boolean isReminded) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = todoService.updateRemindStatus(id, userId, isReminded);
        return Result.success(result);
    }
} 